<html>
<head><meta charset="utf-8"><title>How to get a function&#x27;s args and name from a `DefId`? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html">How to get a function&#x27;s args and name from a `DefId`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231199728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20a%20function%27s%20args%20and%20name%20from%20a%20%60DefId%60%3F/near/231199728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henry Boisdequin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html#231199728">(Mar 21 2021 at 07:29)</a>:</h4>
<p>I have a <code>DefId</code> of a certain function and I would like to get all of its arguments names and the name of the function itself. What would be the best way to do this?</p>
<p>Example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">hello</span>: <span class="nb">String</span><span class="p">,</span><span class="w"> </span><span class="n">hello1</span>: <span class="nb">String</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">args</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="s">"hello"</span><span class="p">,</span><span class="w"> </span><span class="s">"hello1"</span><span class="p">]</span><span class="w"></span>
<span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"foo"</span><span class="w"></span>
</code></pre></div>



<a name="231214605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20a%20function%27s%20args%20and%20name%20from%20a%20%60DefId%60%3F/near/231214605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html#231214605">(Mar 21 2021 at 12:50)</a>:</h4>
<p>Maybe <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.fn_sig">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.fn_sig</a> ?</p>



<a name="231214691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20a%20function%27s%20args%20and%20name%20from%20a%20%60DefId%60%3F/near/231214691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html#231214691">(Mar 21 2021 at 12:52)</a>:</h4>
<p>Err actually no, I think you want tcx.fn_arg_names()</p>



<a name="231214771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20a%20function%27s%20args%20and%20name%20from%20a%20%60DefId%60%3F/near/231214771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html#231214771">(Mar 21 2021 at 12:54)</a>:</h4>
<p>And then you can get the name of the function itself with tcx.item_name()</p>



<a name="231219722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20a%20function%27s%20args%20and%20name%20from%20a%20%60DefId%60%3F/near/231219722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henry Boisdequin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20a.20function&#x27;s.20args.20and.20name.20from.20a.20.60DefId.60.3F.html#231219722">(Mar 21 2021 at 14:32)</a>:</h4>
<p>Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>